package dane.brown.sql.checker;

import dane.brown.sql.checker.service.MyBatisSyntaxCheckerCore;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.apache.commons.cli.*;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Profile;

import java.io.File;
/*
-Dspring.config.additional-location=file:./application.ymlbackup 指定外部的配置文件
 */
@Log4j2
@SpringBootApplication

public class Main implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplicationBuilder application = new SpringApplicationBuilder(Main.class);
        application.web(WebApplicationType.NONE);
        application.run(args);
    }
    @Autowired
    MyBatisSyntaxCheckerCore checkerCore;
    @Override
    public void run(String... args) throws Exception {

        Options options = new Options();
        Option action_config = new Option("c", "config", true, "额外规则配置");
        Option actionMapperLocation = new Option("m", "MapperLocation", true, "Mapper文件的路径");
        options.addOption(action_config);
        options.addOption(actionMapperLocation);
        CommandLineParser parser = new DefaultParser();
        CommandLine cmd = parser.parse(options, args);
        if(cmd.hasOption("c")) {
            checkerCore.loadCheckRules(cmd.getOptionValue("c"));
        }
        String path = cmd.getOptionValue("m");
        File dir = new File(path);
        checkerCore.traverseAndParseXmlFiles(dir);
        checkerCore.checkMappedStatements(path);

    }


//    private void check(String... args){
//        // 解析命令行参数（支持指定配置文件路径）
//        String configFilePath = "oracle_rules.properties"; // 默认配置文件
//        String dirPath = null;
//
//
//        if (args.length == 0) {
//            log.error("请指定MyBatis XML映射文件所在目录！");
//            log.error("用法：java MyBatisOracleSyntaxChecker [目录路径]");
//            return;
//        }
//
//        for (int i = 0; i < args.length; i++) {
//            if ("-c".equals(args[i]) && i + 1 < args.length) {
//                configFilePath = args[i + 1];
//                i++; // 跳过配置文件值
//            } else {
//                dirPath = args[i];
//            }
//        }
//        if (dirPath == null) {
//            System.out.println("请指定MyBatis XML映射文件所在目录！");
//            System.out.println("用法：java MyBatisOracleSyntaxChecker [-c 配置文件路径] [目录路径]");
//            System.out.println("示例：java MyBatisOracleSyntaxChecker -c custom_rules.properties ./src/main/resources");
//            return;
//        }
//        File dir = new File(dirPath);
//        if (!dir.exists() || !dir.isDirectory()) {
//            System.err.println("错误：目录不存在或不是有效目录 -> " + dirPath);
//            return;
//        }
//    }
}
